﻿using System;
using System.Collections.Generic;
using System.Text;
using SerializationHelperLib;
using System.Xml;
using System.IO;
using System.Xml.Serialization;


namespace ConfigMgmt
{
    public class ConfSettings : XmlSerializableDictionary<string, object>
    {
        public ConfSettings()
        {
        }

        public string GetString(string id)
        {
            return (string)this[id];
        }

        public void AddKey(string id, object val)
        {
            if (this.ContainsKey(id))
                this[id] = val;
            else
                this.Add(id, val);
        }

        public void SaveAsXml(string fn)
        {
            XmlSerializer ser = new XmlSerializer(typeof(ConfSettings));
            using (TextWriter tw = new StreamWriter(fn))
            {
                ser.Serialize(tw, this);
            }
        }

        public static ConfSettings CreateFromXml(string fn)
        {
            ConfSettings o = new ConfSettings();
            XmlSerializer ser = new XmlSerializer(typeof(ConfSettings));
            using (TextReader tr = new StreamReader(fn))
            {
                o = (ConfSettings)ser.Deserialize(tr);
            }
            return o;
        }

    }
}
